﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

/// <summary>
/// SysMenuDao 的摘要说明
/// </summary>
public class SysMenuDao
{
	public SysMenuDao()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    //添加
    public static void add(string par_id, string menuName, string menuUrl, string menuOrder, string menuShow)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = "insert into sys_Menu (par_id,menuName,menuUrl,menuOrder,menuShow) values(" + par_id + ",'" + menuName + "','" + menuUrl + "'," + menuOrder + "," + menuShow + ")";
            try { comm.ExecuteNonQuery(); }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
    }
    //更新
    public static void update(string id,string par_id, string menuName, string menuUrl, string menuOrder, string menuShow)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = "update sys_Menu set par_id=" + par_id + ",menuName='" + menuName + "',menuUrl='" + menuUrl + "',menuOrder=" + menuOrder + ",menuShow=" + menuShow + " where id="+id;
            try { comm.ExecuteNonQuery(); }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
    }
    //删除(递归删除子栏目及此栏目)
    public static void del(string id)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            //查询是否有子栏目，如果有则先删除子栏目
            comm.CommandText = "select * from sys_Menu where par_id=" + id;
            try {
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = comm;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++) {
                    del(dt.Rows[i]["id"].ToString());
                }

                //删除此栏目
                comm.CommandText = "delete from sys_menu where id="+id;
                comm.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
    }
}
